/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseBusinessCardDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseBusinessCardDao : BaseDao, IBaseDao
    {
        public BaseBusinessCardDao()
        {
            base.CurrentTableName = BaseBusinessCardTable.TableName;
        }

        public BaseBusinessCardDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseBusinessCardDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseBusinessCardDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseBusinessCardEntity businessCardEntity)
        {
            return this.AddEntity(businessCardEntity);
        }

        public string AddEntity(BaseBusinessCardEntity businessCardEntity)
        {
            if (string.IsNullOrEmpty(businessCardEntity.ID) || string.IsNullOrEmpty(businessCardEntity.SortCode))
            {
                string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseBusinessCardTable.TableName);
                if (string.IsNullOrEmpty(businessCardEntity.ID))
                {
                    businessCardEntity.ID = sequence;
                }
                if (string.IsNullOrEmpty(businessCardEntity.SortCode))
                {
                    businessCardEntity.SortCode = sequence;
                }
            }
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseBusinessCardTable.TableName);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldID, businessCardEntity.ID);
            this.SetEntity(sqlBuilder, businessCardEntity);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldCreateUserRealname, base.UserInfo.Realname);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseBusinessCardTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return businessCardEntity.ID;
        }

        public BaseBusinessCardEntity GetEntity(string id)
        {
            return new BaseBusinessCardEntity(this.Get(id));
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseBusinessCardEntity businessCardEntity)
        {
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldFullName, businessCardEntity.FullName);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldTitle, businessCardEntity.Title);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldCompany, businessCardEntity.Company);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldPhone, businessCardEntity.Phone);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldPostalcode, businessCardEntity.Postalcode);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldMobile, businessCardEntity.Mobile);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldAddress, businessCardEntity.Address);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldEmail, businessCardEntity.Email);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldOfficePhone, businessCardEntity.OfficePhone);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldQQ, businessCardEntity.QQ);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldFax, businessCardEntity.Fax);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldWeb, businessCardEntity.Web);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldBankName, businessCardEntity.BankName);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldBankAccount, businessCardEntity.BankAccount);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldTaxAccount, businessCardEntity.TaxAccount);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldPersonal, businessCardEntity.Personal ? 1 : 0);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldSortCode, businessCardEntity.SortCode);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldDescription, businessCardEntity.Description);
        }

        public int Update(BaseBusinessCardEntity businessCardEntity)
        {
            return this.UpdateEntity(businessCardEntity);
        }

        public int UpdateEntity(BaseBusinessCardEntity businessCardEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseBusinessCardTable.TableName);
            this.SetEntity(sqlBuilder, businessCardEntity);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldModifyUserRealname, base.UserInfo.Realname);
            sqlBuilder.SetValue(BaseBusinessCardTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseBusinessCardTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseBusinessCardTable.FieldID, businessCardEntity.ID);
            return sqlBuilder.EndUpdate();
        }
    }
}